package com.quzhi.sys.controller.orderController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quzhi.common.Result;
import com.quzhi.common.code.Code;
import com.quzhi.sys.entity.OrderLog;
import com.quzhi.sys.entity.User;
import com.quzhi.sys.service.impl.OrderLogServiceImpl;
import com.quzhi.sys.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器 订单日志
 * </p>
 *
 * @author Xibing
 * @since 2024-08-06
 */

@Slf4j
@RestController
@RequestMapping("/sys/orderLog")
public class OrderLogController {

    @Autowired
    private OrderLogServiceImpl orderLogService;

    @Autowired
    private UserServiceImpl userService;


    @PostMapping()
    public Result<?> addLog(@RequestBody Map<String, Object> logs, HttpServletRequest request) {
        // 分割订单日志
        try {
            // 获取当前用户信息
            String token = request.getHeader("qz-Token");
            User userinfoForBack = userService.getUserinfoForBack(token);

            if (userinfoForBack == null) return Result.fail(Code.UPDATE_FAIL_CODE, "用户信息错误");
            List<String> types = (List<String>) logs.get("types");
            String log = (String) logs.get("log");
            String id = (String) logs.get("id");

            // 查询该订单的所有日志 判断是否有重复日志内容，有就不允许添加
            LambdaQueryWrapper<OrderLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OrderLog::getOrderId, id);
            List<OrderLog> list = orderLogService.list(lambdaQueryWrapper);

            if (list != null) {
                // 查重
                for (OrderLog orderLog : list) {
                    if (orderLog.getLog().equals(log)) {
                        return Result.fail(Code.UPDATE_FAIL_CODE, log + ",该日志已存在");
                    }

                    if (types != null) {
                        StringBuilder message = new StringBuilder();
                        for (String type : types) {
                            if (orderLog.getLog().equals(type)){
                                message.append(type).append(",");
                            }

                        }
                        if(!message.toString().equals("")){
                            return Result.fail(Code.UPDATE_FAIL_CODE, message + "该日志已存在,请取消勾选。");
                        }

                    }


                }
            }

            // 查重没问题则添加日志
            assert types != null;
            for (String type : types) {
                orderLogService.AddUserLog(id, type, userinfoForBack.getUsername());
            }


            if (log != null && !log.equals("")) {
                orderLogService.AddUserLog(id, log, userinfoForBack.getUsername());
            }

            return Result.success("日志添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail(Code.UPDATE_FAIL_CODE, "数据格式错误");
        }


    }

}
